#include "HufArc.h"

__HufTree * MakeHufTree (__HufTree ** H_T)
{
	__HufTree * elm_new = NULL;
	__HufTree * head = NULL;
	if ((H_T[0]) && (H_T[1]))
	{ 
		elm_new = (__HufTree *) calloc (1,sizeof(__HufTree));
		if (!elm_new)
			cl_exit (MAKE_HUF_TREE,MEMORY_ERR);
		elm_new->left = H_T[0];
		H_T[0]->top = elm_new;
		elm_new->right = H_T[1];
		H_T[1]->top = elm_new;
		elm_new->count = H_T[0]->count;
		elm_new->count += H_T[1]-> count;
		elm_new->flag |= 1;
		H_T[0] = NULL;
		H_T[1] = NULL;
		if (H_T[2])
			H_T = paste (H_T,elm_new);
		else
			return elm_new;
		head = MakeHufTree (H_T);
	} else
		if (!H_T[1])
		{
			elm_new = (__HufTree *) calloc (1,sizeof(__HufTree));
			if (!elm_new)
				cl_exit (MAKE_HUF_TREE,MEMORY_ERR);
			elm_new->left = H_T[0];
			H_T[0]->top = elm_new;
			elm_new->flag |= 1;
			elm_new->right = (__HufTree*) calloc (1,sizeof(__HufTree));
			elm_new->right->top = elm_new;
			elm_new->right->flag = 1;//
			return elm_new;
		}
		else
			cl_exit (MAKE_HUF_TREE,INCORRECT_DATA,elm_new);
	return head;
}